package helperClassSites;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utils.Utils;

public class CreateStreetviewReturn {
	public static void CreateStreetviewFiles(String path, int xlsSheet) {

		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(xlsSheet);
			Cell[] count = sheet.getColumn(0);

			for (int i = 1; i < count.length; i++) {
				Cell StreetviewCell = sheet.getCell(47, i);
				String streetView = StreetviewCell.getContents();
				if (!streetView.endsWith("NOP")) {

					Cell siteNameCell = sheet.getCell(9, i);
					String sitename = siteNameCell.getContents();
					File replacedFile = new File(path + sitename + "-reverse.html");
					replacedFile.delete();
					FileWriter fstream = new FileWriter(path + sitename + "-reverse.html");
					BufferedWriter textFile = new BufferedWriter(fstream);

					textFile.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN''http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html>  \n");
					textFile.write("<head>  \n");
					textFile.write("<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />\n");
					Cell mountainNameCell = sheet.getCell(4, i);
					String mountainNameEN = mountainNameCell.getContents();
					String replaced = Utils.replaceUmlautsDeutsch(mountainNameEN);
					textFile.write("  <meta name='Description' content='Streetview tour of the " + replaced + " from the other side. This streetview tour shows the complete " + replaced
							+ " in Streetview. There are around 480 passes on www.cyclingthealps.com. Come and have a look.' /> \n");
					textFile.write("  <meta http-equiv='content-type' content='text/html; charset=utf-8' />  \n");
					textFile.write("<style type=\"text/css\">\n");
					textFile.write("  html { height: 100% }\n");
					textFile.write("  body { height: 100%; margin: 0px; padding: 0px }\n");
					textFile.write("  #map_canvas { height: 100% }\n");
					textFile.write("</style>\n");
					textFile.write("<title>Streetview tour of " + replaced + "</title> \n");
					textFile.write("<script type=\"text/javascript\">var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script>");
					textFile.write("    \n");
					textFile.write("<script type=\"text/javascript\">  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-13084846-1']);  _gaq.push(['_trackPageview']);  (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>");
					textFile.write("    \n");
					textFile.write("</head> \n");
					textFile.write("<body>\n");
					textFile.write(streetView);
					textFile.write("</body>  \n");
					textFile.write("</html>  \n");
					textFile.close();
				}
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
